#include "dd_line.h"

#include "util/mmgr.h"		// Needs to come in early

using namespace dd;

Line::Line()
{
}

Line::~Line()
{

}

void Line::Render(DBG_BUFFER* _pBuffer)
{
	AddLine(_pBuffer, m_From, m_To, m_Color); 
	if (m_Arrow)
	{
		AddLine(_pBuffer, m_To, m_ArrowPoints[0], m_Color); 
		AddLine(_pBuffer, m_To, m_ArrowPoints[1], m_Color); 
		AddLine(_pBuffer, m_To, m_ArrowPoints[2], m_Color); 
		AddLine(_pBuffer, m_To, m_ArrowPoints[3], m_Color); 
	}

}

void Line::Setup(const math::Vec3& _From, const math::Vec3& _To, COLOR _Color, bool _Arrow)
{
	m_Color = _Color;
	m_From	= _From;
	m_To	= _To;
	m_Arrow = _Arrow;
	if (m_Arrow)
	{
		// Draw some arrow head ?
		math::Vec3	Delta  = m_To - m_From;
		float	Length = (m_To - m_From).Magnitude();
				Delta  /= Length;
		math::Vec3	Angle = math::Vec3(0, 1, 0);
		float Dot = (Delta ^ Angle);
		if ( Dot > 0.99f)
		{
			Angle = math::Vec3(1, 0, 0);
		}
		math::Vec3 Segment = m_To - (Delta * Length * 0.2f);
	

		// Generate each arrow-head segment by taking the cross
		// product of the previous segment and the main body
		// this will give us vectors at 90 degrees to the body
		// and each subsequent vector 90 degrees of the previous
		math::Vec3 Cross = Delta * Angle;
		for (int i=0; i<3; i++)
		{
			m_ArrowPoints[i] = Cross + Segment;
			Cross = Delta * Cross;
		}
		m_ArrowPoints[3] = Cross + Segment;
	}
}
